package com.kobe.algorithm.leetcode.case107;

import java.util.*;

/**
 * @author ：caopengfei
 * @date ：2021/2/25
 */
public class Soution107S {

    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if(root == null){
            return result;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            List<Integer> level = new ArrayList<>();
            int n = queue.size();
            for (int i = 0; i < n ; i++) {
                TreeNode node = queue.poll();
                level.add(node.val);
                if( node.left != null){
                    queue.offer(node.left);
                }
                if( node.right != null){
                    queue.offer(node.right);
                }
            }
            result.add(0,level);
        }
        return result;
    }



}
